package gui.listeners;

import gfx.hdr.Exposure;
import gfx.hdr.ImageHDR;
import gfx.layers.Layer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JCheckBox;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class LayerListItemListener implements ChangeListener, ActionListener, MouseListener{
	private Layer layer;
	private int oldSliderValue;
	
	public LayerListItemListener(Layer layer){
		this.layer = layer;
	}

	@Override
  public void stateChanged(ChangeEvent e) {
		if(e.getSource() instanceof JCheckBox)
			layer.setSelected(((JCheckBox)e.getSource()).isSelected());
  }

	@Override
  public void actionPerformed(ActionEvent e) {
	  if(e.getActionCommand().equals("SHOW")){
	  	layer.getLayerManager().showLayerInViewport(layer);
	  }
  }

	@Override
  public void mouseClicked(MouseEvent e) {}

	@Override
  public void mousePressed(MouseEvent e) {
		if(e.getSource() instanceof JSlider){
			JSlider slider = (JSlider)e.getSource();
			oldSliderValue = slider.getValue();
		}
  }

	@Override
  public void mouseReleased(MouseEvent e) {
		if(e.getSource() instanceof JSlider){
			JSlider slider = (JSlider)e.getSource();
			if(oldSliderValue != slider.getValue()){
				oldSliderValue = slider.getValue();
				ImageHDR iHDR = (ImageHDR)layer;
				iHDR.setExposure(Exposure.sliderToExposure(slider.getValue()));
				layer.getLayerManager().showLayerInViewport(iHDR);
			}
		}
  }

	@Override
  public void mouseEntered(MouseEvent e) {}

	@Override
  public void mouseExited(MouseEvent e) {}

}
